package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.reportinfoItem;
/**
 * Created by Administrator on 2017/3/25.
 */

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.EntityPropertyMapMapper;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.ReportinfoItemMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.reportinfoItem.JsmReportinfoItemService;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 卢曼成
 * @Description 汇报内容与数据项对应关系实现接口类
 * @CreateDateTime 2017-03-25 13:07
 **/

public class JsmReportinfoItemServiceImpl implements JsmReportinfoItemService {

    @Autowired
    private ReportinfoItemMapper reportinfoItemMapper;

    @Autowired
    private EntityPropertyMapMapper entityPropertyMapMapper;

    /**
     * 根据ID删除
     *
     * @param reportinfoId
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Long reportinfoId) {
        return reportinfoItemMapper.deleteByPrimaryKey(reportinfoId);
    }

    /**
     * 添加
     *
     * @param record
     * @return
     */
    @Override
    public int insert(ReportinfoItem record) {
        return reportinfoItemMapper.insert(record);
    }

    /**
     * 非空添加
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(ReportinfoItem record) {
        return reportinfoItemMapper.insertSelective(record);
    }

    /**
     * 根据ID查询
     *
     * @param reportinfoId
     * @return
     */
    @Override
    public ReportinfoItem selectByPrimaryKey(Long reportinfoId) {
        return reportinfoItemMapper.selectByPrimaryKey(reportinfoId);
    }

    /**
     * 非空修改
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(ReportinfoItem record) {
        return reportinfoItemMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 修改
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKey(ReportinfoItem record) {
        return reportinfoItemMapper.updateByPrimaryKey(record);
    }

    /**
     * @param record
     * @return List<ReportinfoItem>
     * @Author 卢曼成
     * @Description 根据条件查询集合
     * @CreateDateTime 2017/3/24 19:38
     **/
    @Override
    public List<ReportinfoItem> selectByParamList(ReportinfoItem record) {
        return reportinfoItemMapper.selectByParamList(record);
    }

    /**
     * @param record
     * @return List<ReportinfoItem>
     * @Author 卢曼成
     * @Description 根据条件查询集合带有子项name名称
     * @CreateDateTime 2017/3/24 19:38
     **/
    @Override
    public List<ReportinfoItem> selectByParamItemList(ReportinfoItem record) {
        return reportinfoItemMapper.selectByParamItemList(record);
    }

    /**
     * @param record
     * @return List<ReportinfoItem>
     * @Author 卢曼成
     * @Description 根据条件分页查询
     * @CreateDateTime 2017/3/24 19:38
     **/
    @Override
    public List<ReportinfoItem> queryList(ReportinfoItem record) {
        return reportinfoItemMapper.queryList(record);
    }

    /**
     * @param record
     * @return List<ReportinfoItem>
     * @Author 卢曼成
     * @Description 根据条件分页查询总条数
     * @CreateDateTime 2017/3/24 19:38
     **/
    @Override
    public int queryCount(ReportinfoItem record) {
        return reportinfoItemMapper.queryCount(record);
    }

    /**
     * @param map
     * @param record
     * @Author 卢曼成
     * @Description 根据条件分页查询总条数
     * @CreateDateTime 2017/3/24 19:38
     **/
    @Override
    public void pageList(Map<String, Object> map, ReportinfoItem record) {
        if(record.getReportinfoId() != null && record.getReportinfoId() > 0){
            List<ReportinfoItem> list = reportinfoItemMapper.queryList(record);
            int num = reportinfoItemMapper.queryCount(record);
            map.put("rows", list);//数据
            map.put("total", num);//数据
        }
    }


    /**
     * 实体特性关联数据项
     * @param entityPropertyMaps
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String,Object>  dataItemaSsociateFeatures(List<EntityPropertyMap> entityPropertyMaps) throws Exception {
        Map<String,Object> result= Maps.newHashMap();
        try{
            for(EntityPropertyMap entityPropertyMap:entityPropertyMaps){
                entityPropertyMapMapper.insertSelective(entityPropertyMap);
            }
            result.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            result.put("msg", "关联成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("特性关联出错");
        }
    }


    /**
     * 得到和数据项已经关联的特性
     * @param params
     * @return
     */
    @Override
    public Integer getCountOfDataItemFeature(Map<String,Object> params) {
       return entityPropertyMapMapper.getCountOfDataItemFeature(params);

    }

    /**
     * 查询当前特性值是否绑定了该汇报点
     *
     * @param entityPro
     */
    @Override
    public int insertPropertyMap(EntityPro entityPro, EmployeeVo employee) throws Exception {
        int resInt = 0;
        // 特性值id , 汇报点id ， 汇报内容id, 数据项id
        if (StringUtils.isNotEmpty(entityPro.getReportInfoId())
                && StringUtils.isNotEmpty(entityPro.getDevicePropertyId())
                && StringUtils.isNotEmpty(entityPro.getItemId())) {
            // 判断是否绑定，如果绑定则不进行操作
            EntityPropertyMap entityPropertyMap = new EntityPropertyMap();
            entityPropertyMap.setReportinfoId(entityPro.getReportInfoId()); // 汇报内容id
            entityPropertyMap.setDevicePropertyId(entityPro.getDevicePropertyId());
            entityPropertyMap.setReportItem(entityPro.getItemId());
            entityPropertyMap.setOperator(employee.getEmployeeId());
            List<EntityPropertyMap> etMaps = entityPropertyMapMapper.selectEntityPropertyMap(entityPropertyMap);
            if (etMaps != null && etMaps.size() > 0) {
                // 如果存在则不进行操作， 不如果不存在，则进行插入操作
            } else {
                entityPropertyMap.setMapId(UniqueUtil.uuid());
                entityPropertyMap.setOpetime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                entityPropertyMap.setmOperator(employee.getEmployeeId());
                entityPropertyMap.setmUpdate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                resInt = resInt + entityPropertyMapMapper.insertSelective(entityPropertyMap);
            }

        }
        // 数据项id  , 汇报点id ,特性值id
        return resInt;
    }

}
